package com.cirkana.viewer.data.route.gpx;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.cirkana.data.route.gpx.IGpxRouteTrack;
import com.cirkana.data.route.gpx.IGpxRouteTrackPoint;
import com.cirkana.serial.bin.BinarySerializationException;
import com.cirkana.serial.bin.BinarySerializationUtil;
import com.cirkana.serial.bin.IBinarySerializable;

public class GpxRouteTrack implements IGpxRouteTrack, IBinarySerializable {

	private String name;
	private String desc;
	private String cmt;
	private List<IGpxRouteTrackPoint> trackPoints = new ArrayList<IGpxRouteTrackPoint>();

	// TODO: Should be protected
	public GpxRouteTrack() {
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getDesc() {
		return desc;
	}

	@Override
	public void setDesc(String desc) {
		this.desc = desc;
	}

	@Override
	public String getCmt() {
		return cmt;
	}

	@Override
	public void setCmt(String cmt) {
		this.cmt = cmt;
	}

	@Override
	public List<IGpxRouteTrackPoint> getTrackPoints() {
		return trackPoints;
	}

	@Override
	public void setTrackPoints(List<IGpxRouteTrackPoint> trackPoints) {
		this.trackPoints = trackPoints;
	}

	@Override
	public void serialize(DataOutputStream out) throws BinarySerializationException {
		BinarySerializationUtil.serializeString(this.name, out);
		BinarySerializationUtil.serializeString(this.desc, out);
		BinarySerializationUtil.serializeString(this.cmt, out);
		BinarySerializationUtil.serializeSerializableList(this.trackPoints, out);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void deserialize(DataInputStream in) throws BinarySerializationException {
		this.name = BinarySerializationUtil.deserializeString(in);
		this.desc = BinarySerializationUtil.deserializeString(in);
		this.cmt = BinarySerializationUtil.deserializeString(in);
		this.trackPoints = (List<IGpxRouteTrackPoint>) BinarySerializationUtil.deserializeSerializableList(GpxRouteTrackPoint.class, in);
	}

	@Override
	public String toString() {
		StringBuilder s = new StringBuilder("{");

		s.append("name=");
		if (this.name == null)
			s.append("(null)");
		else
			s.append(this.name);
		s.append(",desc=");
		if (this.desc == null)
			s.append("(null)");
		else
			s.append(this.desc);
		s.append(",cmt=");
		if (this.cmt == null)
			s.append("(null)");
		else
			s.append(this.cmt);
		s.append(",trackPoints=");
		if (this.trackPoints == null)
			s.append("(null)");
		else {
			s.append("{");
			for (IGpxRouteTrackPoint trackPoint : this.trackPoints) {
				if (trackPoint == null)
					s.append("(null)");
				else
					s.append(trackPoint.toString());
				s.append(",");
			}
			s.delete(s.length() - 1, s.length());
			s.append("}");
		}

		s.append("}");
		return s.toString();
	}

}
